﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChartTools
{
    /// <summary>
    /// 多折线图
    /// </summary>
    public class MSLine
    {
        private List<string> labels;
        private List<Series> Datas;
        private string Function;
        private string ShowDivId;
        public MSLine(List<string> pLabels, List<Series> pData, string pFunctionName, string pShowDivId)
        {
            labels = pLabels;
            Datas = pData;
            ShowDivId = pShowDivId;
            Function = pFunctionName;
        }
        private MSLine() { }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<chart caption='' subcaption='' lineThickness='1' showValues='1' formatNumberScale='0' anchorRadius='2'   divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridColor='CC3300' shadowAlpha='40' labelStep='2' numvdivlines='5' chartRightMargin='35' bgColor='FFFFFF' bgAngle='270' bgAlpha='10,10' alternateHGridAlpha='5'  legendPosition ='RIGHT '>");
            sb.Append("<categories>");
            foreach (string label in labels)
            {
                sb.Append("<category label='" + label + "' />");
            }
            sb.Append("</categories>");
            foreach (Series data in Datas)
            {
                sb.Append("<dataset seriesName='" + data.Name + "' color='" + data.Color + "' anchorBorderColor='" + data.Color + "' anchorBgColor='" + data.Color + "'>");
                foreach (decimal d in data.Datas)
                {
                    sb.Append("	<set value='" + d + "' />");
                }

                sb.Append("</dataset>");
            }
            sb.Append("<styles><definition><style name='CaptionFont' type='font' size='12'/></definition><application><apply toObject='CAPTION' styles='CaptionFont' /><apply toObject='SUBCAPTION' styles='CaptionFont' /></application></styles></chart> ");
            string str = sb.ToString();
            sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\">");
            sb.Append("            function ");
            sb.Append(Function);
            sb.AppendLine("(){");
            sb.AppendLine("                var chart = new FusionCharts(\"/Content/FusionCharts/MSLine.swf\", \"ChartId\", \"100%\", \"298\", \"0\", \"0\");");
            sb.AppendLine("                chart.setXMLData(\"" + str + "\");");
            sb.AppendLine("                chart.render(\"" + ShowDivId + "\");");
            sb.AppendLine("             }");
            sb.AppendLine("        </script>");
            return sb.ToString();
        }
    }

    public class MSLineData
    {
        public List<string> Labels { get; set; }
        public List<Series> Datas { get; set; }
    }

    public class Series
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public List<double> Datas { get; set; }

    }
}
